package com.inspire.ijc.agent.utils;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author ：inspire
 * @date ：Created in 2023/12/13 11:06
 * @description：使用队列实现栈的操作
 * @modified By：inspire
 * @version:
 */
public class QueueToStack<T> {
    private Queue<T> queue;
    private Queue<T> help;

    public QueueToStack() {
        queue = new LinkedList<T>();
        help = new LinkedList<T>();
    }

    public void push(T value) {
        queue.offer(value);
    }

    public T pop() {
        while (queue.size() > 1) {
            help.offer(queue.poll());
        }
        T ans = queue.poll();
        Queue<T> temp = queue;// 此时queue为空
        queue = help;
        help = temp;
        return ans;
    }

    public int size(){
        return queue.size();
    }

    public boolean empty() {
        return queue.size() == 0;
    }
}
